package com.yxy.springcloud.framework.unionauth.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.base.PageDataList;
import com.yxy.springcloud.framework.unionauth.DTO.AppQueryDto;
import com.yxy.springcloud.framework.unionauth.dao.AppMapper;
import com.yxy.springcloud.framework.unionauth.model.App;
import com.yxy.springcloud.framework.unionauth.model.AuthAdminUser;
import com.yxy.springcloud.framework.unionauth.model.InsertBaseEntity;
import com.yxy.springcloud.framework.unionauth.utils.TokenCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class AppService {
  @Autowired
  private TokenCheckUtil tokenCheckUtil;

  @Resource
  private AppMapper appMapper;

  public PageDataList<App> getList(AppQueryDto query) {
    List<App> resultList = new ArrayList<>();

    PageHelper.startPage(query.getPageNumber(), query.getPageSize());
    PageHelper.orderBy("create_date desc");//排序

    App queryObj = new App();
    BeanUtils.copyProperties(query, queryObj);
    List<App> dbResult = appMapper.select(queryObj);
    Page<App> dbResultPage = (Page<App>) dbResult;

    for (App dbItem : dbResultPage.getResult()) {
      resultList.add(dbItem);
    }

    PageDataList<App> pageDataList = new PageDataList<>(dbResultPage.getTotal(),
            dbResultPage.getPages(),
            dbResultPage.getPageNum(), dbResultPage.getPageSize());
    pageDataList.setList(resultList);
    return pageDataList;

  }

  @Transactional(rollbackFor = Exception.class)
  public Map<String, Object> createOrUpdate(App inputDto) throws BusinessException {
    Map<String, Object> resultMap = new HashMap<>();

    String curUserStr = tokenCheckUtil.getUserInfoByToken();
    AuthAdminUser loginUser = JSON.parseObject(curUserStr, AuthAdminUser.class);
    Long id = inputDto.getId();
    if (id == null) { // insert
      App insertObj = new App();
      BeanUtils.copyProperties(inputDto, insertObj);
      InsertBaseEntity baseEntity = new InsertBaseEntity(loginUser.getId(), loginUser.getName());
      BeanUtils.copyProperties(baseEntity, insertObj);
      appMapper.insertUseGeneratedKeys(insertObj);
      resultMap.put("id", insertObj.getId());
    } else { // update
      App updateObj = new App();
      BeanUtils.copyProperties(inputDto, updateObj);
      updateObj.setId(id);
      updateObj.setUpdateUserId(loginUser.getId());
      updateObj.setUpdateUserName(loginUser.getName());
      updateObj.setUpdateDate(new Date());
      appMapper.updateByPrimaryKeySelective(updateObj);
      resultMap.put("id", id);
    }
    return resultMap;
  }

  // 删除app将 删除与之关联的 角色\租户等关联关系
  @Transactional(rollbackFor = Exception.class)
  public void delete(long id) throws BusinessException {
    App app = appMapper.selectByPrimaryKey(id);
    if (app == null) {
      throw new BusinessException(501, "找不到该条记录");
    }
    appMapper.deleteByPrimaryKey(id);
    // todo 删除角色 租户与之的关联关系
  }
}
